package guiservice.server.controls;

import guiservice.server.logic.UidGenerator;
import lombok.extern.log4j.Log4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@Log4j
public class ObjectFactory {

    public static <T> T populateObject(long owner, Class<T> cl) {
        try {
            Class cls = Class.forName(cl.getCanonicalName());
            Constructor<T> ctor = cls.getConstructor(long.class, long.class);
            ctor.setAccessible(true);
            return ctor.newInstance(UidGenerator.getInstance().getNextUid(), owner);
        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
            log.error("Can not create object of class type " + cl.getCanonicalName(), e);
            return null;
        }
    }

}
